<?php

namespace app\erp\controller;

use app\erp\model\Sitedb;
use app\erp\model\Staffdb;

class Member extends Base
{

    protected $cols = array(
        array(
            'field' => 'member_id',
            'width' => 40,
            'title' => 'ID',
            'type' => 'checkbox',
            'fixed' => 'left'
        ),
        array(
            'field' => 'member_id',
            'width' => 60,
            'title' => 'id'
        ),
        array(
            'field' => 'username',
            'width' => 160,
            'title' => '用户名',
            'sort' => 'true'
        ),
        array(
            'field' => 'company_name',
            'width' => 160,
            'title' => '公司',
            'sort' => 'true'
        ),
        array(
            'field' => 'site_id_text',
            'width' => 160,
            'title' => '分站',
            'sort' => 'true'
        ),
        array(
            'field' => 'realname',
            'width' => 160,
            'title' => '姓名',
            'sort' => 'true'
        ),
        array(
            'field' => 'sex',
            'width' => 100,
            'title' => '性别',
            'sort' => 'true'
        ),
        array(
            'field' => 'phone',
            'width' => 160,
            'title' => '联系电话',
            'sort' => 'true'
        ),
        array(
            'field' => 'tel',
            'width' => 160,
            'title' => '座机',
            'sort' => 'true'
        ),
        array(
            'field' => 'email',
            'width' => 160,
            'title' => '邮箱',
            'sort' => 'true'
        ),
        array(
            'field' => 'create_time',
            'width' => 120,
            'title' => '注册时间',
            'sort' => 'true'
        ),
        array(
            'field' => 'last_login_time',
            'width' => 120,
            'title' => '上次登录时间'
        ), array(
            'field' => 'last_ip',
            'width' => 120,
            'title' => '上次登录IP'
        ),
        array(
            'field' => 'state',
            'width' => 100,
            'title' => '会员状态',
            'templet' => '#stateTpl'
        ),
        array(
            'fixed' => 'right',
            'width' => 80,
            'toolbar' => '#member_tool'
        )
    );
    protected $colsLog = array(
        array(
            'field' => 'id',
            'width' => 40,
            'title' => 'ID',
            'type' => 'checkbox',
            'fixed' => 'left'
        ),
        array(
            'field' => 'login_sys',
            'width' => 160,
            'title' => '登录系统'
        ),
        array(
            'field' => 'username',
            'width' => 160,
            'title' => '用户名'
        ),
        array(
            'field' => 'login_ip',
            'width' => 160,
            'title' => '登录IP'
        ),
        array(
            'field' => 'login_time',
            'width' => 160,
            'title' => '登录时间'
        ),
        array(
            'field' => 'agent',
            'width' => 800,
            'title' => '登录设备'
        )
    );
    public $post;

    public function _initialize()
    {
        parent::_initialize();
        $request = request();
        $param = $request->param();
        $this->post = $param;
        // 调用分站数据
        $sitedb = new Sitedb();
        $sitedb->param = array(
            '_o' => 'site_id',
            '_b' => 'asc',
            'page' => 1,
            'limit' => 1000
        );
        $get_site_list = $sitedb->ajaxSelect();
        $this->assign('get_site_list', $get_site_list['data']);
    }

    /**
     *
     * @todo 保存数据
     */
    public function saveData()
    {
        $staffdb = new Staffdb();
        $staffdb->param = $this->post;
        $return = $staffdb->ajaxSave();
        if ($return != 'success') {
            $this->layuiReturn($return, 0, $return, -1);
        } else {
            $this->layuiReturn('保存成功');
        }
    }

    public function index()
    {
        $this->assign('cols', json_encode($this->cols));
        return $this->fetch();
    }

    // 登录日志
    public function loginlog()
    {
        $this->assign('cols', json_encode($this->colsLog));
        return $this->fetch();
    }

    // 异步获取日志
    public function ajaxlog()
    {
        $memberLogdb = new \app\erp\model\MemberLogdb();
        $memberLogdb->param = $this->post;
        $return = $memberLogdb->order($this->post['_o'], $this->post['_b'])->paginate($this->post['limit']);
        //halt($return->items());
        $this->layuiReturn($return->items(), $return->total());
    }
}